<template>
  <div class="photoinfo-container">
    <h3>{{ photoinfo.title }}</h3>
    <p class="subtitle">
      <span>发表时间: {{ photoinfo.add_time | dataFormat }}</span>
      <span>点击次数： {{ photoinfo.click }}</span>
    </p>
    <hr>

    <!-- 缩略图区域 -->
    <vue-preview :list="list" @close="handleClose"></vue-preview>

    <!-- 图片内容区域 -->
    <div class="content" v-html="photoinfo.content"></div>

    <!-- 评论子组件 -->
    <cmt-box :id="id"></cmt-box>
  </div>
</template>

<script>
import comment from "../subcomponents/comment.vue";

export default {
  data() {
    return {
      id: this.$route.params.id, // 从路由中获取到的图片id
      photoinfo: {}, // 图片详情
      list: [] // 缩略图数组
    };
  },
  created() {
    this.getPhotoInfo();
    this.getThumbs();
  },
  methods: {
    getPhotoInfo() {
      //  获取图片详情
      this.$http
        .get("api/getimageInfo/" + this.id) // 后端接口
        .then(result => {
          if (result.body.status === 0) {
            this.photoinfo = result.body.message[0];
          } else {
            Toast("获取图片详情失败！");
          }
        });
    },
    getThumbs() {
      //  获取缩略图
      this.$http
        .get("api/getthumimages/" + this.id) // 后端接口
        .then(result => {
          if (result.body.status === 0) {
            //   循环每个图片数据，补全图片的宽高
            result.body.message.forEach(item => {
              item.w = 600;
              item.h = 400;
            });
            this.list = result.body.message;
          } else {
            Toast("获取图片详情失败！");
          }
        });
    },
    handleClose() {
      console.log("close event");
    }
  },
  components: {
    // 注册评论子组件
    "cmt-box": comment
  },
};
</script>

<style lang="scss" scoped>
.photoinfo-container {
  padding: 4px;
  h3 {
    color: #26a2ff;
    font-size: 14px;
    text-align: center;
    margin: 10px 0;
  }
  .subtitle {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
  }
  .content {
    font-size: 13px;
    line-height: 24px;
  }
}
</style>
